import { getCategoryList } from '@/services/modules/category';
import { categoryItem } from '@/types';
import { Effect, Reducer } from 'umi';

export interface CategoryModelState {
  CategoryItem: categoryItem[]
}

export interface CategoryModelType {
  namespace: 'category';
  state: CategoryModelState;
  effects: {

    getCategoryList: Effect;
  };
  reducers: {
    save: Reducer<CategoryModelState>;
  };

}

const PageModel: CategoryModelType = {
  namespace: 'category',
  state: {
    CategoryItem: []
  },
  //副作用
  effects: {
    *getCategoryList({ payload }, { call, put }) {
      const result = yield call(getCategoryList)
      if (result.data) {
        yield put({
          type: 'save',
          payload: {
            CategoryItem: result.data

          }
        })
      }
    }
  },
  //同步修改state的操作
  reducers: {
    save(state, action) {
      return {
        ...state,
        ...action.payload,
      };
    },

  },

};

export default PageModel;